<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="H5活动信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :label-width="formLabelWidth"
    >
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="dialogForm.timeData"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="截止日期"
        />
      </el-form-item>
      <el-form-item label="图片">
        <el-upload
          :action="uploadimgUrl"
          :data="{ project_code: 'mobybox' }"
          :show-file-list="false"
          :on-success="handleUploaderImgSuccess"
          :limit="1"
          :headers="TOKENT_HEADER"
          class="avatar-uploader"
        >
          <img v-if="dialogForm.img" :src="dialogForm.img" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="链接">
        <el-input v-model="dialogForm.url" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
import { uploadimgUrl } from "@/api/system/uploadimg";
import { TOKENT_HEADER } from "@/config/api-constant/system";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      img: "",
      url: "",
      timeData: [],
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits([
  "closeDialog",
  "cancel",
  "submitForm",
  "handleUploaderImgRequest",
]);

const formLabelWidth = "120px";

const handleUploaderImgSuccess = (res) => {
  emits("handleUploaderImgRequest", res);
};
// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        const param = Object.assign({}, dialogForm);
        param.start_time = dialogForm.timeData[0]
          ? dialogForm.timeData[0].valueOf()
          : "";
        param.end_time = dialogForm.timeData[1]
          ? dialogForm.timeData[1].valueOf()
          : "";
        delete param.timeData;

        emits("submitForm", param);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
</script>
